<template>
  <div class="login">
    <h2>海洋启明星后台管理系统</h2>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px">
      <el-form-item label="帐号" prop="admin" class="white-label">
        <el-input type="text" v-model="ruleForm.admin"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" class="white-label">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { login } from 'network/login'
  export default {
    name: 'Login',
    data() {
      // 验证admin的合法性
      var validateAdmin = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入帐号'))
        }
        callback()
      }
      // 验证密码的合法性
      var validatePwd = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入密码'))
        } 
        if (value.length < 6 || value.length > 10) {
          return callback(new Error('密码长度为6-10位'))
        }
        callback()
      }
      return {
        // 表单数据
        ruleForm: {
          admin: '',
          password: ''
        },
        // 表单验证规则
        rules: {
          admin: [
            { validator: validateAdmin, trigger: 'blur' }
          ],
          password: [
            { validator: validatePwd, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      // 提交表单
      submitForm(formName) {
        let {admin, password} = this.ruleForm
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // 表单数据合法，发送网络请求，进行登录
            this.login(admin, password)
          } else {
            console.log('error submit!')
            return false
          }
        })
      },
      // 重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      // 登录 网络请求
      login(admin, password) {
        login(admin, password).then(res => {
          // 账号密码不正确时，返回错误信息
          if (res.status) alert(res.message)
          // 账号密码正确，登录成功，跳转到首页
          else {
            // 将管理员账号与 token 存入 sessionStorage
            sessionStorage.setItem('admin', admin)
            sessionStorage.setItem('token', res.token)
            // 放入 vuex
            this.$store.dispatch('setAdmin', admin)
            // 需要放在最后
            this.$router.push('/home')
          }
        })
      }
    }
  }
</script>

<style>
  .login {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    height: 745px;
    background-color: #121738;
    background-size: 100%;
    padding-top: 150px;
  }

  h2 {
    color: #fff;
    margin: 0 0 50px 40%;
  }

  .el-form {
    width: 350px;
    margin-left: 35%;
  }

  .el-form-item {
    margin-bottom: 50px;
  }
  
  .white-label .el-form-item__label {
    color: #fff;
    font-size: 16px;
  }

  .el-form-item:nth-child(3) {
    margin-left: 30px;
  }
</style>